import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { PROP_TYPE, TIP_ENUMS } from '../../../../common/src/util/Enums';
import { AudioManager } from '../../../../common/src/manager/AudioManager';
import { UIManager } from '../../../../framework/ui/uimanager/UIManager';
import { MonoUIPanel } from '../../../../common/src/mono/MonoUIPanel';
import DataMgr from '../../../../common/src/util/DataMgr';
import Config from '../../../../common/src/util/Config';
import { WECHAT } from 'cc/env';
import SDKManager from '../../../../common/src/manager/SDKManager';
const { ccclass, property } = _decorator;

@ccclass('PropertyDialog')
export class PropertyDialog extends MonoUIPanel {
    @property({ type: Label, displayName: "顶部标题" })
    title: Label = null;

    @property({ type: Label, displayName: "信息" })
    info: Label = null;

    @property({ type: Label, displayName: "金额" })
    money: Label = null;

    @property({ type: [SpriteFrame], displayName: "图标的卡片" })
    iconFrames: SpriteFrame[] = []
    
    @property({ type: Sprite, displayName: "信息" })
    currentIcon: Sprite = null;
    
    // 当前类型
    private propType: PROP_TYPE = PROP_TYPE.WASH
    
    // --------------------------- 外部调用 --------------------------- //
    public async setData(data) {
        this.propType = data.type
        this.initUI()
    }

    // --------------------------- 内部调用 --------------------------- //
    private initUI() {
        if(this.propType){
            switch(this.propType){
                case PROP_TYPE.WASH:
                    this.title.string = "洗牌道具"
                    this.info.string = "将场上所有的字块全\n部回收并重新发放"
                    this.currentIcon.spriteFrame = this.iconFrames[0]
                    break;
                case PROP_TYPE.MAGNET:
                    this.title.string = "凑齐道具"
                    this.info.string = "自动凑齐合成栏中\n可消除的一对成语"
                    this.currentIcon.spriteFrame = this.iconFrames[1]
                    break;
                case PROP_TYPE.CLEAN:
                    this.title.string = "移除道具"
                    this.info.string = "将合成栏中所有字\n块放回到场上"
                    this.currentIcon.spriteFrame = this.iconFrames[2]
                    break;
                case PROP_TYPE.ADD:
                    this.title.string = "加时道具"
                    this.info.string = "使用后可增加120s"
                    this.currentIcon.spriteFrame = this.iconFrames[3]
                    break;    
            }
        }
    }

    // 结算
    private closeResult() {
        if(this.propType){
            switch(this.propType){
                case PROP_TYPE.WASH:
                    break;
                case PROP_TYPE.MAGNET:
                    break;
                case PROP_TYPE.CLEAN:
                    break;
                case PROP_TYPE.ADD:
                    break;    
            }
        }

        this.onClosePanel()
    }

    // --------------------------- 点击事件 --------------------------- //
    /** 关闭面板. */
    private async onClosePanel() {
        await this.onExitAnimation()
        UIManager.close("property:res/prefab/PropertyDialog");
    }

    // 使用金币
    private onUsingMoney() {
        AudioManager.playButton()
        if (DataMgr.money >= Config.videoAddMoney) {
            DataMgr.money = DataMgr.money - Config.videoAddMoney
            this.closeResult()
        } else {
            UIManager.open("tip:res/prefab/TipPanel", (err, res:Node) => {
                res.getComponent("TipPanel").setData(TIP_ENUMS.MONEY_NOT_ENOUGH)
            });
        }
    }

    // 点击看视频
    private onLookVideo() {
        AudioManager.playButton()
        if (WECHAT) {
            SDKManager.showRewardVideoAd("使用道具: " + this.propType, (err, res) => {
                if (!err) {
                    this.closeResult()
                } else {
                    UIManager.open("tip:res/prefab/TipToast", (err, node:Node) => {
                        if(res.code == 1001){ 
                            node.getComponent("TipToast").setData(TIP_ENUMS.LOAD_VIDEO_FAIL)
                        }else{
                            node.getComponent("TipToast").setData(TIP_ENUMS.NOT_FULL_VIDEO)
                        }
                    });  
                }
            })
        } else {
            this.closeResult()
        }
    }
}


